package gemini.castor.ui.server.service.impl;

import java.util.ArrayList;
import java.util.List;

import gemini.basic.dto.SearchDistributorCriteria;
import gemini.basic.dto.SearchDistributorResult;
import gemini.basic.exception.UserExistedException;
import gemini.basic.manager.DistributorManager;
import gemini.basic.model.Distributor;
import gemini.castor.ui.client.service.DistributorService;
import gemini.castor.ui.server.service.AbstractSingleService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DistributorServiceImpl extends
        AbstractSingleService<DistributorManager> implements
        DistributorService {

    @SuppressWarnings("unused")
	private final Log logger = LogFactory
            .getLog(DistributorServiceImpl.class);

    public Distributor update(Distributor distributor){
    	Distributor result = duplicate(getServerManager().init(distributor));
    	return result;
    }
    
    public Distributor save(){
    	Distributor result = duplicate(getServerManager().getPlaceHolder());
    	return result;
    }

	@Override
	public Distributor getByCode(String code) {
		Distributor result = duplicateExcludeFields(getServerManager().getByCode(code));
    	return result;
	}

	@Override
	public Distributor getDistributorByCodePin(String code, String pin) throws UserExistedException {
		Distributor result = duplicateExcludeFields(getServerManager().getDistributorByCodePin(code, pin));
    	return result;
	}

	@Override
	public ArrayList<SearchDistributorResult> findDistributor(
			SearchDistributorCriteria criteria) {
		List<SearchDistributorResult> resultList = getServerManager().findDistributor(criteria);
		ArrayList<SearchDistributorResult> result = null;
		
		if(resultList != null && !resultList.isEmpty()){
			result = new ArrayList<SearchDistributorResult>(resultList.size());
			for (SearchDistributorResult rs : resultList) {
				result.add(duplicate(rs));
			}
		}
		
		return result;
	}
}
